package org.example.sb_20241005springboot.controller;

import org.example.sb_20241005springboot.model.Person;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:做渐进主义的改良派,不做完没注意的速成派
 * User: XALEXC
 * Date: 2024-10-07
 * Time: 18:39
 */
@RequestMapping("/param")
@RestController
public class ParamController {

    //接收一个参数
    @RequestMapping("/m1")
    public String m1(String name){
        return "接收到的参数是name:"+name;
    }

    //接收多个参数,使用包装类
    @RequestMapping("/m2")
    public String m2(String name,Integer age){
        return "接收到的参数是name:"+name+",age:"+age;
    }

    //接收多个参数,不使用包装类
    @RequestMapping("/m3")
    public String m3(String name,int age){
        return "接收到的参数是name:"+name+",age:"+age;
    }

    //传递对象数据
    @RequestMapping("/m4")
    public String m4(Person person){
        return "接收到的参数是person:"+person.toString();

    }

    //重命名操作
    @RequestMapping("/m5")
    public String m5(@RequestParam(value = "name",required = false) String username){
        return "接收到的参数是name:"+username;
    }

    //传递数组
    @RequestMapping("/m6")
    public String m6(String[] arrays){
        return "接收到的参数是arrays:"+ Arrays.toString(arrays);
    }

    //传递集合类
    @RequestMapping("/m7")
    public String m7(@RequestParam(required = false) List<String> list){
        return "接收到的参数是list:"+ list;
    }

    //传递JSON数据
    @RequestMapping("/m8")
    public String m8(@RequestBody Person person) {
        return "接收到的数据是person:"+person.toString();
    }

    //拿取URL的数据
    @RequestMapping("/m9/{UserId}/{name}")
    public String m9(@PathVariable Integer UserId,@PathVariable("name") String username){
        return "接收到的数据是UserId:"+UserId.toString()+",username:"+username;
    }

    //传递文件数据
    @RequestMapping("/m10")
    public String m10(@RequestPart MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:/temp/"+file.getOriginalFilename()));
        return "success";
    }

    //获取单个cookie数据
    @RequestMapping("/getCookie")
    public String getCookie(@CookieValue String aaa){
        return "cookie中存取的值:"+aaa;
    }

    //获取多个cookie数据
    @RequestMapping("/getCookie2")
    public String getCookie2(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null){
            for(Cookie cookie:cookies){
                System.out.println(cookie.getName()+":"+cookie.getValue());
            }
        }
        return "获取全部cookie成功";
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("username","zhangsan");
        return "session设置成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session=request.getSession(false);
        if(session!=null){
            String username1=session.getAttribute("username").toString();
            String username2=(String)session.getAttribute("username");
            System.out.println(username1);
            System.out.println(username2);
            return "获取到的session是:"+session.getAttribute("username").toString();
        }
        return "session为空";
    }

    //使用spring的方式获取session
    @RequestMapping("/getSession2")
    public String getSession2(@SessionAttribute(required = false) String username){
        return "username:"+username;
    }

    @RequestMapping("/getSession3")
    public String getSession3(HttpSession session){
        String username=(String)session.getAttribute("username");
        return "username:"+username;
    }

    //获取header
    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String UserAgent=request.getHeader("User-Agent");
        return "User-Agent:"+UserAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String UserAgent){
        return "User-Agent:"+UserAgent;
    }
}
